package service

import (
	"context"
	"diygw-ui-go/internal/v1/dao"
	"diygw-ui-go/internal/v1/model/admin"
	"fmt"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"time"
)

type sysDataBase struct{}

// 对象实例化
func SysDataBase() *sysDataBase {
	return &sysDataBase{}
}

// 获取列表不分页
func (s *sysDataBase) GetList(ctx context.Context, in admin.SysDatabaselistInput) (out admin.SysDatabaselistOutput, err error) {
	db := g.DB()
	rs, _ := db.GetAll(ctx, fmt.Sprintf(`select table_name as name,table_comment as comment,engine,table_collation as collation from INFORMATION_SCHEMA.Tables where table_schema = %s`, "diygw2024"))
	var _list []admin.SysDatabaseOneInput
	rs.Structs(&_list)
	return
}

// 获取列表分页
func (s *sysDataBase) GetListPage(ctx context.Context, in admin.SysDatabaselistInput) (out admin.SysDatabaselistOutput, err error) {
	db := g.DB()
	rs, _ := db.GetAll(ctx, fmt.Sprintf(`select table_name as name,table_comment as comment,engine,table_collation as collation from INFORMATION_SCHEMA.Tables where table_schema = '%s'`, "diygw2024"))
	//fmt.Println("这里是", _err)
	rs.Structs(&out.List)
	out.Total = len(out.List)
	return
}

// 获取详细信息,缓存10小时
func (s *sysDataBase) GetOne(ctx context.Context, in admin.SysConfigOneInput) (out *admin.SysConfigOneInput, err error) {
	err = dao.SysConfig.Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour * 10,
		Name:     "configID-" + gconv.String(in.ConfigId),
		Force:    false,
	}).Where("config_id", in.ConfigId).Scan(&out)
	return
}
